home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Settle a bet please
- Date: Wed, 03 Apr 96 23:39:20 GMT
- Organization: none
- Message-ID: <828574760snz@genesis.demon.co.uk>
- References: <4jfopb$o9n@news1.sympatico.ca> <4jh8rtINNosd@mayne.ugrad.cs.ubc.ca> <4jp8li$ue@eri2.erinet.com> <828542716snz@genesis.demon.co.uk> <4jufbc$erj@eri.erinet.com>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4jufbc$erj@eri.erinet.com> timb@erinet.com "Tim Berens" writes:
-
- >>>{
- >>> printf("size is: %d",sizeof("My Name"));
- >>>}
-
- >>Remember sizeof doesn't produce a result of type int, rather size_t which
- >>is some implementation-dependent unsigned type.
-
- >Do people participate in this newsgroup simply to show their smarts by
- >desperately seeking nits to pick?
-
- Not generally, as far as I can tell.
-
- >Also, even though it is remotely possible that size_t is not the same
- >size as an int and that the compiler may not automatically cast it to
- >an int for the function call Or that the size of "My name" will exceed
- >32K and size_t will be a 2 byte unsigned integer,
-
- size_t can't be an int so %d can never be the correct format specifier to
- use for it.
-
- I've never come across a compiler which attempts to convert an argument in
- a variable argument list to an appropriate type. The language has very
- specific rules as to what a compiler should do with such an argument so
- the compiler can perform such a conversion if it can detect with certainty
- a case of undefined behaviour. That is possible in this case with a standard
- library function but having done so the much better approach would be to
- warn about the problem rather than bodge it. In fact a number of compilers
- do this. So one of the problems with the code is that it simply won't
- compile cleanly on some compilers (e.g. gcc with suitable options).
-
- sizeof("My Name") is guaranteed to be 8 and representable as an int which is
- why I cast to int in this case. In the general case the correct solution
- is to use the %lu format specifier and cast to unsigned long.
-
- > does it really matter in this example?
-
- Yes, it is the difference between well written code that has a well defined
- meaning from the language spec. and low maintenance and poorly written
- code whose behaviour is undefined by the language and is a time bomb waiting
- to go off.
-
- comp.lang.c should ideally be a newsgroup where you can learn something about
- the correct use of the C language. That means that bad usage should not go
- unchallenged.
-
- > Can you name a compiler where
- >printf("size=%d",sizeof("My Name")); will not properly display
- >"size=8"?
-
- Not offhand but that is not really important. What is important is whether
- the code actually means anything or not. If you are building or porting large
- project you have to be able to trust its individual components.
-
- However I could easily envisage a 64 bit system where size_t is a 64 bit
- unsigned long and int is 32 bit. Possibly DOS compilers that implement a
- huge memory model may have size_t 32 bits and int 16 bits.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-